package com.qfedu.filiter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author han tao
 * @version 1.0
 * @date 2023/4/10 14:46
 * @project day33-web-common
 */
@WebFilter("/*")
public class CharacterFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //转换类型
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        HttpServletResponse resp= (HttpServletResponse) servletResponse;
        //解决中文乱码
        req.setCharacterEncoding("UTF-8");
        //如果请求的是html不执行
        //获取请求的所有资源路径
        /*String requestURI = req.getRequestURI();
        if (requestURI.endsWith(".html")){
            //解决响应的中文乱码    会导致html中出现乱码
            resp.setContentType("text/html;charset=utf-8");
        }*/
        // 放行
        filterChain.doFilter(req,resp);
    }

    @Override
    public void destroy() {

    }
}
